package com.zenith.common.result;

import com.zenith.common.constant.CommonConstant;
import lombok.Data;

import java.io.Serializable;

/**
 * 统一响应结果
 */
@Data
public class Result<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 状态码
     */
    private Integer code;

    /**
     * 返回信息
     */
    private String message;

    /**
     * 返回数据
     */
    private T data;

    /**
     * 判断是否成功
     * @return true表示成功，false表示失败
     */
    public boolean isSuccess() {
        return CommonConstant.SUCCESS.equals(this.code);
    }

    public static <T> Result<T> success() {
        return success(null);
    }

    public static <T> Result<T> success(T data) {
        return success("操作成功", data);
    }

    public static <T> Result<T> success(String message, T data) {
        return result(CommonConstant.SUCCESS, message, data);
    }

    public static <T> Result<T> fail() {
        return fail("操作失败");
    }

    public static <T> Result<T> fail(String message) {
        return fail(message, null);
    }

    public static <T> Result<T> fail(String message, T data) {
        return result(CommonConstant.FAIL, message, data);
    }
    public static Result<?> fail(int code, String message) {
        return result(code, message, null);
    }
    // 未认证
    public static <T> Result<T> unauthorized(String message) {
        return result(CommonConstant.UNAUTHORIZED, message, null);
    }
    // 无权限
    public static <T> Result<T> forbidden(String message) {
        return result(CommonConstant.FORBIDDEN, message, null);
    }
    // 返回结果
    private static <T> Result<T> result(Integer code, String message, T data) {
        Result<T> result = new Result<>();
        result.setCode(code);
        result.setMessage(message);
        result.setData(data);
        return result;
    }
}